﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using System;
using DG.Tweening;
public class SkillManager : LogicModuleBase, ISkillManager
{
    /// <summary>
    /// 添加技能
    /// </summary>
    /// <param name="mono"></param>
    public void AddSkill(Mono mono,SkillBase skill)
    {
        if (!mono.SkillList.Contains(skill))
            mono.SkillList.Add(skill);
    }
    /// <summary>
    /// 移除技能
    /// </summary>
    /// <param name="mono"></param>
    public void RemoveSkill(Mono mono, SkillBase skill)
    {
        if (!mono.SkillList.Contains(skill))
            mono.SkillList.Remove(skill);
    }

    public override void FixedUpdate()
    {
        base.FixedUpdate();
    }

    /// <summary>
    /// CD逻辑
    /// </summary>
    public void CountCD(SkillBase skill)
    {
        if (skill.CD.CurrentCD <= 0)
        {
            skill.CD.CurrentCD = 0;
        }
        else
        {
            GameMgr.Get<ITimeManager>().TimeGoing();
            skill.CD.CurrentCD -= Time.deltaTime;
        }
    }

    /// <summary>
    /// 获得初始化的技能
    /// </summary>
    public SkillBase InitSkillData(int skillID)
    {
        SkillBase SB = new SkillBase();
        SB.SkillTypeID = Data.Instance.TableAgent.GetInt("AllSkill", skillID.ToString(), "SkillTypeID");
        SB.CD.CDTime = float.Parse(Data.Instance.TableAgent.GetString("AllSkill", skillID.ToString(), "CD"));
        SB.CD.CurrentCD = 0;
        SB.skillMPData.MPUse = Data.Instance.TableAgent.GetInt("AllSkill", skillID.ToString(), "MPUse");
        SB.Projectile = Data.Instance.TableAgent.GetString("AllSkill", skillID.ToString(), "Projectile");
        SB.Name = Data.Instance.TableAgent.GetString("AllSkill", skillID.ToString(), "Name");
        SB.IconName = Data.Instance.TableAgent.GetString("AllSkill", skillID.ToString(), "IconName");
        SB.SkillAttr = (MonoAttribute)Data.Instance.TableAgent.GetInt("AllSkill", skillID.ToString(), "Attr");
        return SB;
    }

    #region 具体技能函数
    public void UseSkill(Mono mono,SkillBase SB, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr, float FixedRange)
    {
        if(SB.SkillAttr != MonoAttribute.None)
        {
            Attr = SB.SkillAttr;
        }
        if (SB.SkillTypeID == 0)
        {
            BulletSkillAffect(mono, SB.Projectile, Pos, Dir, LaucherRange, Attr, FixedRange);
        }
        if (SB.SkillTypeID == 1)//近战
        {
            mono.monoRealtimeStatus.IsCounterAttack = true;
            GameMgr.Get<IMonoManager>().GetObj(mono).GetComponent<CircleCollider2D>().offset += new Vector2(Dir.normalized.x, Dir.normalized.y)*0.6f;
            GameMgr.Get<IMonoManager>().GetObj(mono).transform.Find("Sprite").DOLocalMove(Dir.normalized * 0.9f, 0.15f).OnComplete(() => {
                GameMgr.Get<IMonoManager>().GetObj(mono).transform.Find("Sprite").DOLocalMove(Vector3.zero, 0.2f).OnComplete(()=> {
                    mono.monoRealtimeStatus.IsCounterAttack = false;
                });
                GameMgr.Get<IMonoManager>().GetObj(mono).GetComponent<CircleCollider2D>().offset = Vector2.zero;
            });
            BulletSkillAffect(mono, SB.Projectile, Pos, Dir, LaucherRange, Attr, FixedRange);
        }
        if (SB.SkillTypeID == 2)//三散弹
        {
            BulletSkillAffect(mono, SB.Projectile, Pos, RotateRound(Dir, Vector3.forward, 20), LaucherRange, Attr, FixedRange);
            BulletSkillAffect(mono, SB.Projectile, Pos, RotateRound(Dir, Vector3.forward, -20), LaucherRange, Attr, FixedRange);
            BulletSkillAffect(mono, SB.Projectile, Pos, Dir, LaucherRange, Attr, FixedRange);
        }
        if (SB.SkillTypeID == 3)//散射弹
        {
            float Ang = UnityEngine.Random.Range(-8, 8);
            BulletSkillAffect(mono, SB.Projectile, Pos, RotateRound(Dir, Vector3.forward, Ang), LaucherRange, Attr, FixedRange);
        }
        if (SB.SkillTypeID == 4)//散射弹
        {
            mono.Direction = Dir;
            GameMgr.Get<IMonoManager>().GetObj(mono).GetComponent<MonoBase>().LastTimeMove(Dir, (mono.Speed / GameMgr.Get<IMonoManager>().GetObj(mono).GetComponent<MonoBase>().FreezeRate) * 10, 12, () => { });
        }
    }
    public static Vector3 RotateRound(Vector3 direction, Vector3 axis, float angle)
    {
        Vector3 NewDirect = Quaternion.AngleAxis(angle, axis) * direction;
        return NewDirect;
    }

    /// <summary>
    /// 子弹类技能
    /// </summary>
    public void BulletSkillAffect(Mono mono, string projectile, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr, float FixedRange)
    {
        GameMgr.Get<IBulletManager>().AddMono(projectile, GetInitBulletData(mono,projectile, Pos, Dir, LaucherRange, Attr, FixedRange));
    }
    public void BulletSkillAffect(string projectile, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr)
    {
        GameMgr.Get<IBulletManager>().AddMono(projectile, GetInitBulletData(projectile, Pos, Dir, LaucherRange, Attr));
    }
    public void BulletSkillAffect(string projectile, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr, float FixedRange)
    {
        GameMgr.Get<IBulletManager>().AddMono(projectile, GetInitBulletData(projectile, Pos, Dir, LaucherRange, Attr, FixedRange));
    }
    #endregion

    /// <summary>
    /// 初始化bullet数据
    /// </summary>
    private Bullet GetInitBulletData(Mono mono, string Name, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr, float FixedRange)
    {
        Bullet b = new Bullet();
        b.TypeID = Data.Instance.TableAgent.GetInt("AllBullet", Name, "TypeID");
        b.Damage = Data.Instance.TableAgent.GetInt("AllBullet", Name, "Damage") + mono.buffStatus.DefaultAttckAdd;
        b.Speed = Data.Instance.TableAgent.GetFloat("AllBullet", Name, "Speed");
        b.Range = (FixedRange > Data.Instance.TableAgent.GetInt("AllBullet", Name, "Range")) ? Data.Instance.TableAgent.GetInt("AllBullet", Name, "Range") : FixedRange;
        b.AddBuffNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "AddBuffNum") + mono.buffStatus.AddAtrrNum;
        b.collision.MaxNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "MaxNum");
        b.collision.NowNum = 0;
        b.positionData.Position = Pos + (Dir.normalized) * LaucherRange;
        b.positionData.StartPosition = Pos;
        b.Direction = Dir;
        b.attribute = Attr;
        return b;
    }
    private Bullet GetInitBulletData(string Name, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr)
    {
        Bullet b = new Bullet();
        b.TypeID = Data.Instance.TableAgent.GetInt("AllBullet", Name, "TypeID");
        b.Damage = Data.Instance.TableAgent.GetInt("AllBullet", Name, "Damage");
        b.Speed = Data.Instance.TableAgent.GetFloat("AllBullet", Name, "Speed");
        b.Range = Data.Instance.TableAgent.GetInt("AllBullet", Name, "Range");
        b.AddBuffNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "AddBuffNum");
        b.collision.MaxNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "MaxNum");
        b.collision.NowNum = 0;
        b.positionData.Position = Pos + (Dir.normalized) * LaucherRange;
        b.positionData.StartPosition = Pos;
        b.Direction = Dir;
        b.attribute = Attr;
        return b;
    }
    private Bullet GetInitBulletData(string Name, Vector3 Pos, Vector3 Dir, float LaucherRange, MonoAttribute Attr, float FixedRange)
    {
        Bullet b = new Bullet();
        b.TypeID = Data.Instance.TableAgent.GetInt("AllBullet", Name, "TypeID");
        b.Damage = Data.Instance.TableAgent.GetInt("AllBullet", Name, "Damage");
        b.Speed = Data.Instance.TableAgent.GetFloat("AllBullet", Name, "Speed");
        b.Range = FixedRange;
        b.AddBuffNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "AddBuffNum");
        b.collision.MaxNum = Data.Instance.TableAgent.GetInt("AllBullet", Name, "MaxNum");
        b.collision.NowNum = 0;
        b.positionData.Position = Pos + (Dir.normalized) * LaucherRange;
        b.positionData.StartPosition = Pos;
        b.Direction = Dir;
        b.attribute = Attr;
        return b;
    }
}
public class SkillBase
{
    public enum SkillType
    {
        bullet,
        move,
        buff,
    }
    public int SkillTypeID =0;
    public SkillMPData skillMPData = new SkillMPData();
    public SkillType skilltype
    {
        get
        {
            switch (SkillTypeID)
            {
                case 0: return SkillType.bullet;
                case 1: return SkillType.move;
                case 2: return SkillType.buff;
                default: return SkillType.bullet;
            }
        }
    }
    public CDdata CD = new CDdata();
    public string Projectile = "";
    public string Name = "";
    public string IconName = "";
    public MonoAttribute SkillAttr = MonoAttribute.None;
}
public class SkillMPData
{
    public int MPUse;
}
public class CDdata
{
    public float CDTime = 0;
    public float CurrentCD = 0;
}